<cfcomponent output="false">
	
	<!--- --->

	<cffunction name="listPlayers" access="public" output="false" returntype="query">
		<cfargument name="userID" required="true" />

		<cfreturn application.g.pongGateway.listPlayers(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveUserRatings" access="public" output="false" returntype="void">
		
		<cfset var local = {} />
		
		<cfset local.user = application.s.userService.getUserByID(session.userID) />
		<cfset local.userID = local.user.id />

		<cfif local.user.recordcount eq 0>
			<cfset application.q.addMessage("Please Log in to enter ratings.") />
		<cfelse>
			<cfloop collection="#arguments#" item="local.item">
				<cfif local.item contains 'rating_'>
					<cfset local.playerID = right(local.item,len(local.item)-7) />
					<cfset local.rating = arguments[local.item] />
					<cfset saveUserRating(userID=local.userID,playerID=local.playerID,rating=local.rating) />
				</cfif>
				<cfif local.item contains 'rule_'>
					<cfset local.ruleID = right(local.item,len(local.item)-5) />
					<cfset local.ruleOptionID = arguments[local.item] />
					<cfset saveUserVote(userID=local.userID,ruleID=local.ruleID,ruleOptionID=local.ruleOptionID) />
				</cfif>
			</cfloop>
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveUserRating" access="public" output="false" returntype="void">
		<cfargument name="userID" required="true" />
		<cfargument name="playerID" required="true" />
		<cfargument name="rating" required="true" />
		
		<cfset var local = {} />
		
		<cfif isNumeric(arguments.rating) and arguments.rating neq ''>
			<cfif arguments.rating gt 100>
				<cfset arguments.rating = 100 />
			</cfif>
			<cfif arguments.rating lt 0>
				<cfset arguments.rating = 0 />
			</cfif>
			<cfquery name="local.existingRecord" datasource="fundrdb">
				select id from
				pong_rate_user
				where user_id = #application.q.queryID(arguments.userID)#
				and player_id = #application.q.queryID(arguments.playerID)#
			</cfquery>
			<cfset application.q.dbsave(
				table="pong_rate_user",
				id=local.existingRecord.id,
				user_ID=arguments.userID,
				player_ID=arguments.playerID,
				rating=arguments.rating
			) />
		</cfif>
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getRules" access="public" output="false" returntype="query">
		
		<cfreturn application.g.pongGateway.getRules(session.user.id) />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveUserVote" access="public" output="false" returntype="void">
		<cfargument name="userID" required="true" />
		<cfargument name="ruleID" required="true" />
		<cfargument name="ruleOptionID" required="true" />
		
		<cfset var local = {} />
		
		<cfif arguments.ruleID neq '' and arguments.ruleOptionID neq ''>
			<cfquery name="local.clearRecord" datasource="fundrdb">
				delete from pong_vote
				where user_id = '#arguments.userID#'
				and pong_rule_id = '#arguments.ruleID#'
			</cfquery>
			<cfset application.q.dbsave(
				table="pong_vote",
				id="",
				user_ID=arguments.userID,
				pong_rule_id=arguments.ruleID,
				pong_rule_option_id=arguments.ruleOptionID
			) />
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="setupMatches" access="public" output="false" returntype="void">
		
		<cfset var local = {} />
		
		<cfset local.players = listPlayers(userID=session.user.id) />
		
		<cfset _deleteMatches() />
		
		<cfset local.matchID = "" />
		<cfset local.round = 0 />
		<cfset local.sort_order = 0 />
		
		<cfloop query="local.players">
			<cfif application.q.isOdd(local.players.currentRow)>
				<cfset local.sort_order++ />
				<cfset saveMatch(
					id="",
					round=1,
					loser_bracket=0,
					sort_order=local.sort_order
				) />
			</cfif>
		</cfloop>
		
		<cfset generateRound(round=2) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="_deleteMatches" access="private" output="false" returntype="void">
		
		<cfset var local = {} />
		
		<cfquery name="local.deleteMatches" datasource="fundrdb">
			delete from pong match;
		</cfquery>
		
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveMatch" access="public" output="false" returntype="string">
		<cfargument name="id" required="true" />
		<cfargument name="round" required="false" />
		<cfargument name="loser_bracket" required="false" />
		<cfargument name="sort_order" required="false" />
		<cfargument name="user_1_id" required="false" />
		<cfargument name="user_2_id" required="false" />
		<cfargument name="winner_id" required="false" />
		<cfargument name="pong_match_id" required="false" />
		
		<cfset var local = {} />
		
		<cfset local.id = application.q.dbSave(argumentCollection=arguments) />
		
		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
</cfcomponent>